package ru.ipo.dces.plugins.admin.resultstable;

import ru.ipo.dces.client.Controller;
import ru.ipo.dces.clientservercommunication.UserHistoryLine;
import ru.ipo.problemsapi.ExtendedProblem;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 30.03.11
 * Time: 16:16
 */
public class UserHistoryTableModel implements TableModel {

    private UserHistoryLine[] history;

    public UserHistoryTableModel(UserHistoryLine[] history) {
        this.history = history;
    }

    public int getRowCount() {
        return history.length;
    }

    public int getColumnCount() {
        return 4;
    }

    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Время";
            case 1:
                return "Попытка";
            case 2:
                return "Задача";
            case 3:
                return "Результат";
        }
        return null;
    }

    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Date.class;
            case 1:
                return Integer.class;
            case 2:
                return String.class;
            case 3:
                return Map.class;
        }
        return null;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        UserHistoryLine line = history[rowIndex];

        switch (columnIndex) {
            case 0:
                return line.submissionTime;
            case 1:
                return line.attempt;
            case 2:
                return getProblemName(line.problemId);
            case 3:
                return line.results;
        }
        return null;
    }

    //TODO fix, now it works only if connected user is a Participant
    private String getProblemName(int problemId) {
        ExtendedProblem p = Controller.getProblemById(problemId);
        if (p == null)
            return "Задача №" + problemId;
        else
            return p.getName();
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //do nothing
    }

    public void addTableModelListener(TableModelListener l) {
        //do nothing
    }

    public void removeTableModelListener(TableModelListener l) {
        //do nothing
    }
}
